package cn.msqweb.model;

import cn.msqweb.constant.BusinessEnum;
import lombok.Data;

@Data
public class Result<T> {
    private Integer code = 200;
    private String msg = "ok";
    private T data;

    /**
     * 操作成功
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(T data) {
        Result result = new Result();
        result.setData(data);
        return result;
    }

    /**
     * 操作失败
     * @param code
     * @param msg
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(Integer code,String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(null);
        return result;
    }
    /**
     * 操作失败
     * @param businessEnum
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(BusinessEnum businessEnum) {
        Result result = new Result();
        result.setCode(businessEnum.getCode());
        result.setMsg(businessEnum.getDesc());
        result.setData(null);
        return result;
    }

    /**
     * 操作结果
     * @param flag
     * @return
     */
    public static Result<String> handler(Boolean flag) {
        Result result = Result.success(null);
        if (!flag) {
            result = Result.fail(BusinessEnum.OPERATION_FAIL);
        }
        return result;
    }
}
